package com.bookstore.spring.action;

import com.bookstore.spring.pojo.ShoppingCart;
import com.bookstore.spring.service.ShoppingService;
import com.bookstore.spring.web.BookStoreWebUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 2016/8/16.
 */
public class ShoppingAction {
    private ShoppingService shoppingService ;

    /**
     * 查看购物车check
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView("/WEB-INF/page/cart", ModelAndView.FORWARD);
        return mv;
    }

    public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        int id = -1;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
        }

        ShoppingCart sc = BookStoreWebUtils.getShoppingCart(request);
        shoppingService.delete(sc, id);

        if (sc.isEmpty()) {
            ModelAndView mv = new ModelAndView("/WEB-INF/page/emptycart", ModelAndView.FORWARD);
            return mv;
        }
        ModelAndView mv = new ModelAndView("/WEB-INF/page/cart", ModelAndView.FORWARD);
        return mv;
    }

    public ModelAndView clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ShoppingCart sc = BookStoreWebUtils.getShoppingCart(request);
        shoppingService.clear(sc);
        ModelAndView mv = new ModelAndView("/WEB-INF/page/emptycart", ModelAndView.FORWARD);
        return mv;
    }

    public void updateItemQuantity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //4. 在 updateItemQuantity 方法中, 获取 quanity, id, 再获取购物车对象, 调用 service 的方法做修改
        //根据书的id  修改购物车数量,获取id 的值,并且获取修改后数量的值quantity;
        String idStr = request.getParameter("id");
        String quantityStr = request.getParameter("quantity");
        ShoppingCart sc = BookStoreWebUtils.getShoppingCart(request);
        int id = -1;
        int quantity = -1;

        try {
            id = Integer.parseInt(idStr);
            quantity = Integer.parseInt(quantityStr);
        } catch (NumberFormatException e) { }

        if (id > 0 && quantity >= 0) {
            shoppingService.updateItemQuantity(sc, id, quantity);
        }
        //5. 传回 JSON 数据: bookNumber:xx, totalMoney
        Map<String, Object> result = new HashMap<>();
        result.put("bookNumber", sc.getBookNumber());
        result.put("totalMoney", sc.getTotalMoney());

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);

//        Gson gson=new Gson();
//        String json=gson.toJson(result);

        response.setContentType("text/javascript");
        response.getWriter().print(json);

    }

    public ShoppingService getShoppingService() {
        return shoppingService;
    }

    public void setShoppingService(ShoppingService shoppingService) {
        this.shoppingService = shoppingService;
    }
}
